﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for FrmMember.xaml
    /// </summary>
    public partial class FrmMember : Window
    {
        readonly static FrmMember frm = new FrmMember();
        static bool result = false;

        public static string MemberName { get { return frm.txtName.Text; } set { frm.txtName.Text = value; } }
        public static string MemberComment { get { return frm.txtComment.Text; } set { frm.txtComment.Text = value; } }
        public static bool MemberSex { get { return frm.radMale.IsChecked == true; } set { if (value) frm.radMale.IsChecked = true; else  frm.radFemale.IsChecked = true; } }
        public static object MemberImage { get { if (frm.image.EditValue == null) { return null; } return BitmapConversion.ToWinFormsBitmap((BitmapImage)frm.image.EditValue); } set { frm.image.EditValue = value; } }


        private void ResetControl()
        {
            txtName.Text = "";
            txtComment.Text = "";
            radMale.IsChecked = false;
            radFemale.IsChecked = false;
            image.EditValue = null;
            gridHistory.DataSource = null;
        }
        private void AssignData(eKanbanWPF.Model.Member member)
        {
            if (member != null)
            {
                txtName.Text = member.Name;
                txtComment.Text = member.Comment;
                if (member.Sex)
                {
                    radMale.IsChecked = true;
                }
                else
                {
                    radFemale.IsChecked = true;
                }
                try
                {
                    image.EditValue = BitmapConversion.ToWpfBitmap((Bitmap)member.Image);
                }
                catch (Exception)
                {
                    image.EditValue = member.Image;
                }
                
                gridHistory.DataSource = member.History;
                //gridHistory.PopulateColumns();
            }
            else
            {
                ResetControl();
            }
        }

        public static bool Show(eKanbanWPF.Model.Member member, IList<eKanbanWPF.Model.Task> tasks = null)
        {
            result = false;
            frm.AssignData(member);
            //frm.TaskLookUp.ItemsSource = tasks;
            var r = frm.ShowDialog();
            return result;
        }

        private FrmMember()
        {
            InitializeComponent();
            App.Current.MainWindow.Closed += (sender, e) => this.Close();
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.Visibility == System.Windows.Visibility.Visible)
            {
                this.Hide();
                e.Cancel = true; 
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                result = true;
                this.DialogResult = true; 
            }
        }

        private bool Validate()
        {
            var value = txtName.EditValue;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                return false;
                //return new ValidationResult(false, "Member name cannot be empty.");
            }

            if (!radMale.IsChecked==true  && !radFemale.IsChecked==true)
            {
                return false;
            }
            return true;
        }

        private void txtName_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            var value = e.Value;

            e.IsValid = true;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                e.IsValid = false;
                e.ErrorContent = "Member name cannot be empty.";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
                //return new ValidationResult(false, "Member name cannot be empty.");
            }
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
